//
//  HelpViewController.m
//  FengyuanXS
//
//  Created by 王冬 on 2020/7/28.
//  Copyright © 2020 王冬. All rights reserved.
//

#import "HelpViewController.h"
#import "HomeCollectionViewCell.h"
#import "HelpViewCell.h"
#import "FileWebViewController.h"
static const CGFloat kLineSpacing = 1.f;   //列间距 |
static const CGFloat kItemSpacing = 1.f;   //item之间的间距  --
static const NSInteger kRowNumber = 4;     //列数
@interface HelpViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *nameArray;
@property (nonatomic, strong) NSArray *imageArray;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (nonatomic, assign) int page;
@property (nonatomic, copy) NSString *parent_id;
@property (nonatomic, copy) NSString *status;
@end

@implementation HelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"帮扶救助";
    self.tableView.tableFooterView = [UIView new];
    
     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
       layout.minimumLineSpacing    = kLineSpacing;
       layout.minimumInteritemSpacing = kItemSpacing;
       layout.scrollDirection = UICollectionViewScrollDirectionVertical;
       self.collectionView.showsHorizontalScrollIndicator = NO;
       self.collectionView.collectionViewLayout = layout;
       self.collectionView.delegate = self;
       self.collectionView.dataSource = self;
       [self.collectionView  registerNib:[UINib nibWithNibName:@"HomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HomeCollectionViewCell"];
    self.tableView.rowHeight = 100;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"HelpViewCell" bundle:nil] forCellReuseIdentifier:@"HelpViewCell"];
    
    self.imageArray = @[@"jiu_one",@"jiu_two",@"jiu_three",@"jiu_four"];
    self.nameArray = @[@"大病救助",@"法律援助",@"金秋助学",@"困难帮扶"];
    self.parent_id = @"12";
    self.page = 1;
    self.status = @"大病救助";
    self.titleLabel.text = [NSString stringWithFormat:@"栏目: %@",self.status];
    [self getParent_id:self.parent_id page:self.page];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
  //  [self.dataArray removeAllObjects];
    self.navigationController.navigationBar.hidden = NO;
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count;
}

//每个单元格的大小size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((collectionView.width-10-kItemSpacing*(kRowNumber-1))/kRowNumber, 80);
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeCollectionViewCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewCell" forIndexPath:indexPath];
    cell.iconImageView.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
    cell.titleLabel.text = self.nameArray[indexPath.row];
    
    
    return cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.dataArray removeAllObjects];
    self.titleLabel.text = [NSString stringWithFormat:@"栏目: %@",self.nameArray[indexPath.row]];
     self.status = self.nameArray[indexPath.row];
    self.page = 1;
    if (indexPath.row == 0) {
        self.parent_id = @"12";
    }
    if (indexPath.row == 1) {
        self.parent_id = @"13";
    }
    if (indexPath.row == 2) {
        self.parent_id = @"14";
    }
    if (indexPath.row == 3) {
        self.parent_id = @"15";
    }
    [self getParent_id:self.parent_id page:self.page];
    
}







#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HelpViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HelpViewCell" forIndexPath:indexPath];
    DangModel *model = self.dataArray[indexPath.row];
    cell.titleLabel.text = model.title;
    cell.timeLabel.text = [self ConvertStrToTime:model.create_time];
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DangModel *model = self.dataArray[indexPath.row];
       FileWebViewController *webVC = [[FileWebViewController alloc]init];
       
       
       
       webVC.noticeID = model.id;
       webVC.titleStr = self.status;
       
       
       [self.navigationController pushViewController:webVC animated:YES];

}

//时间戳变为格式时间
- (NSString *)ConvertStrToTime:(NSString *)timeStr

{
    
    long long time=[timeStr longLongValue];
    //    如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //    long long time=[timeStr longLongValue] / 1000;
    
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString*timeString=[formatter stringFromDate:date];
    
    return timeString;
    
}




-(void)getParent_id:(NSString *)parent_id page:(int)page{
    WS(weakSelf);
    [[ToastManager shardInstance]showSystemProgress:self.view];
    [[ApiManager ShardInstance]noticeListParentid:parent_id page:page resultBlock:^(NSDictionary *data, NSError *error) {
        BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
        if (model.code == 1) {
            [[ToastManager shardInstance]hideProgress:self.view];
            NSArray *array = [DangModel mj_objectArrayWithKeyValuesArray:data[@"rs"][@"data"]];
            
            weakSelf.tableView.mj_footer.alpha = 0.0;
            [self.dataArray addObjectsFromArray:array];
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            weakSelf.page ++;
        }
    }];
}

@end
